package _1URL类;

import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class TestDemo {
	static Scanner sc;
	static String str = "";
	public static void main(String[] args) throws IOException {

		// URL
//		URL u1 = new URL("https://www.leomessi.cn/");
//		System.out.println(u1);
//		
//		// 1.打开流
//		InputStream is = u1.openStream();
//		Reader r = new InputStreamReader(is, "utf-8");
//		
//		// 2.读取内容
//		char[] buf = new char[1024];
//		int len;
//		StringBuilder sb = new StringBuilder();
//		
//		while((len = r.read(buf))>-1)
//		{
//			sb.append(buf,0,len);
//		}
//		System.out.println(sb.toString());
//		
//		// 3.关闭流
//		r.close();

		// 练习:
		// 1.
		// 让用户输入一个网址,程序获取这个网址的内容，保存为一个文件
		// 测试1 https://war.163.com/photoview/00AO0001/2312767.html
		// 保存为2312767.html
		// 测试2
		// http://pic-bucket.ws.126.net/photo/0001/2021-12-01/GQ48005M00AO0001NOS.jpg
		// 保存为GQ48005M00AO0001NOS.jpg
		// (提示，使用字节流，不需要转换为字符流处理)
		sc = new Scanner(System.in);
//		// 网址
//		System.out.println("请输入一个网址:");
//		String address = sc.next();
//		URL u2 = new URL(address);
//		String path = u2.getPath();
//		String[] split = path.split("/");
//		String filename = split[split.length - 1];
//
//		// 1.打开流
//		InputStream is1 = u2.openStream();
//		Writer os = new FileWriter(filename);
//		Reader r1 = new InputStreamReader(is1, "utf-8");
//		// 2.读取内容
//		char[] buf = new char[1024];
//		int len;
//		while ((len = r1.read(buf)) > -1) {
//			os.write(buf, 0, len);
//		}
//
//		// 3.关闭流
//		is1.close();
//		os.close();

		// 图片
//		System.out.println("请输入一张图片地址:");
//		String pictureaddress = sc.next();
//		URL u3 = new URL(pictureaddress);
//		String path1 = u3.getPath();
//		String[] split1 = path1.split("/");
//		String filename1 = split1[split1.length - 1];
//		System.out.println(filename1);
//
//		// 1.打开流
//		InputStream is2 = u3.openStream();
//		OutputStream os1 = new FileOutputStream(filename1);
////				// 2.读取内容
//		byte[] buf1 = new byte[1024];
//		int len1;
//		while ((len1 = is2.read(buf1)) > -1) {
//			os1.write(buf1, 0, len1);
//		}
////				
////				// 3.关闭流
//		is2.close();
//		os1.close();
	

		// 2.
		// 打印显示当前电脑所在网络的公网ip
		// 方法:
		// 程序访问https://2021.ip138.com/得到网页内容，将ip地址字符串截取出来
		URL u4 = new URL("https://tool.lu/ip");
		InputStream is2 = u4.openStream();
		OutputStream os2 = new FileOutputStream("ip.html");
		byte[] buf2 = new byte[1024];
		int len2;
		while ((len2 = is2.read(buf2)) > -1) {
			os2.write(buf2, 0, len2);
		}
		Reader r1 = new FileReader("ip.html");
		BufferedReader b1 = new BufferedReader(r1);
		while((str = b1.readLine())!=null)
		{
			if(str.contains("ipaddress"))
			{
				String ip = str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));
				System.out.println("ip地址:"+ip);
				break;
			}
		}
		is2.close();
		os2.close();
		r1.close();
		b1.close();
		
		// 3.
		// 打印显示今日无锡天气
		// 打开http://www.weather.com.cn/weather1d/101190201.shtml
		// 读取内容
		// 字符串截取出今日的天气信息
		URL u5 = new URL("http://www.weather.com.cn/weather1d/101190201.shtml");
		InputStream is3 = u5.openStream();
		Reader r3 = new InputStreamReader(is3, "utf-8");
		Writer os3 = new FileWriter("weather.html");
		char[] buf3 = new char[1024];
		int len3;
		while ((len3 = r3.read(buf3)) > -1) {
			os3.write(buf3, 0, len3);
		}
		Reader r2 = new FileReader("weather.html");
		BufferedReader b2 = new BufferedReader(r2);
		while((str = b2.readLine())!=null)
		{
			if(str.contains("hidden_title"))
			{
				String weather = str.substring(46, str.lastIndexOf("\""));
				System.out.println("今日天气:"+weather);
				break;
			}
		}
		is3.close();
		os3.close();
		r2.close();
		b2.close();
		
		

	}
}